//
//  SYAlert.m
//  项目三
//
//  Created by rimi on 16/9/9.
//  Copyright © 2016年 Ivan. All rights reserved.
//

#import "SYAlert.h"
#import "AppDelegate.h"

@implementation SYAlert

+ (void)showNormalAlertWithTitle:(NSString *)title message:(NSString *)message buttonNames:(NSArray<NSString *> *)NamesArray buttonAction:(void (^)(int))buttonAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if ([NamesArray isKindOfClass:[NSArray class]] && NamesArray.count)//判断传进来的数组是否匹配并且存在值
    {
        int index = 0;
        for (NSString *name in NamesArray)
        {
            [alertController addAction:[UIAlertAction actionWithTitle:name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (buttonAction)
                {
                    buttonAction(index);
                }
            }]];
            index ++;
        }
    }
    else
    {
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (buttonAction)
            {
                buttonAction(0);
            }
        }]];
    }
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    [rootVC presentViewController:alertController animated:YES completion:nil];
}

+ (void)showSheetAlertWithTitle:(NSString *)title message:(NSString *)message buttonNames:(NSArray<NSString *> *)NamesArray buttonAction:(void (^)(int))buttonAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    if ([NamesArray isKindOfClass:[NSArray class]] && NamesArray.count)//判断传进来的数组是否匹配并且存在值
    {
        int index = 0;
        for (NSString *name in NamesArray)
        {
            [alertController addAction:[UIAlertAction actionWithTitle:name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (buttonAction)
                {
                    buttonAction(index);
                }
            }]];
            index ++;
        }
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
    }
    else
    {
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (buttonAction)
            {
                buttonAction(0);
            }
        }]];
    }
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    [rootVC presentViewController:alertController animated:YES completion:nil];
}

@end
